function [newr] = autobridge(t0)
%
% automatic bridge correction for a group of traces.
% tries to match v between t0-1 and t0+2 points
global VOLTAGE CURRENT DFILE

newr = 0;
QueMessage('AutoBridge Running');
nr = size(VOLTAGE, 1);
r = zeros(nr, 1);
time = make_time(DFILE);
RATES = ([DFILE.rate(1:nr)] .* DFILE.nr_channel) / 1000; % array of sampling rates, convert to msec
t0r = floor(10./RATES);
for i = 1:nr
   t0=t0r(i);
   dv = VOLTAGE(i,t0-1)-VOLTAGE(i,t0+2);
   di = CURRENT(i,t0-1)-CURRENT(i,t0+2);
   [i; dv; di]';
   if(abs(dv) > 0.5 & abs(di) > 0.5)
      r(i) = dv/di;
   else
      r(i) = 0;
   end;
end;
newr = mean(r);
for i = 1:nr
   VOLTAGE(i,:) = VOLTAGE(i,:) - CURRENT(i,:)*newr; % perform correction
end;
bridge_adjust('display'); % update the display
newr = newr * 1000;
QueMessage('Autobridge Done', 1);
return;
